package com.maxd.service.article;

import com.maxd.model.ArticleCollect;
import com.maxd.respository.article.ArticleCollectJpaRepository;
import com.maxd.respository.article.ArticleCollectRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 收藏
 */
@Service
public class ArticleCollectServiceImpl implements ArticleCollectService {

    @Autowired
    private ArticleCollectJpaRepository articleCollectJpaRepository;
    @Autowired
    private ArticleCollectRepository articleCollectRepository;

    @Override
    public ArticleCollect save(ArticleCollect articleCollect) {
        return articleCollectJpaRepository.save(articleCollect);
    }

    @Override
    public void deleteById(Long articleCollectId) {
        articleCollectJpaRepository.deleteById(articleCollectId);
    }

    @Override
    @Transactional
    public void deleteByIds(String articleCollectIds) {
        for(String str:articleCollectIds.split(",")){
            articleCollectJpaRepository.deleteById(Long.parseLong(str));
        }
    }

    @Override
    public Long selectCount(Long articleId, Long userId) {
        return articleCollectRepository.selectCount(articleId,userId);
    }

    @Override
    public Page<ArticleCollect> selectArticleCollectList(Long userId, Pageable pageable) {
        return articleCollectRepository.selectArticleCollectList(userId,pageable);
    }

    @Override
    public void updateArticleCollect(Long userId,Long articleCollectId){
        Long Id=selectCount(userId,articleCollectId);
        if(Id==null || Id==0){
            ArticleCollect articleCollect=new ArticleCollect();
            articleCollect.setArticleId(articleCollectId);
            articleCollect.setCreateTime(new Date());
            articleCollect.setUserId(userId);
            save(articleCollect);
        }else{
            deleteById(Id);
        }
    }


}
